<template>
    <div class="wrapper">
        <!-- 顶部 -->
        <div class="top">在线商城系统</div>

        <!-- 主体 -->
        <div class="main">
            <div class="left">
                <img src="../assets/login.png" style="width: 100%;">
            </div>
            <div class="right">
                <!-- 标题 -->
                <div class="title">欢迎登录</div>
                <!-- 表单 -->
                <el-form :model="user" :rules="rules" ref="userForm">
                    <el-form-item prop="userName">
                        <el-input v-model="user.userName" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="user.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <div style="display: flex;">      
                            <el-input v-model="code" placeholder="请输入验证码"></el-input>
                            <span @click="refreshCode" style="cursor: pointer;">
                                <Identify :identifyCode="identifyCode"></Identify>    
                            </span>
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="login">登录</el-button>
                        <el-button type="warning" @click="$router.push('/register')">前往注册</el-button>  
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import Identify from '@/components/Identify.vue'
import { setRoutes } from '@/router/index.js'

export default {
    components: {
        Identify
    },
    data() {
        return {
            user: {},
            rules: {
                userName: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
            },
            identifyCode: '', // 验证码
            identifyCodes: '234567890ABCDEFGHJKLMNOPQRSTUVWXYZ', 
            code: ''
        }
    },
    mounted(){
        this.refreshCode()
    },
    methods: {
        // 生成随机验证码
        refreshCode(){
            this.identifyCode = ''
            for(var i=0;i<4;i++){
                this.identifyCode += this.identifyCodes[Math.floor(Math.random()*this.identifyCodes.length)]
            }
        },
        // 登录 
        login(){
            // 判断验证码
            if(this.code.toLowerCase() !== this.identifyCode.toLowerCase()){
                this.$message.error('验证码错误')
                this.refreshCode()
                return
            }
            // 判断表单校验是否合法
            this.$refs.userForm.validate(async valid => {
                // 校验未通过
                if(!valid) return

                // 发送请求
                const res = await this.request.post('/user/login', this.user)
                if(res.status !== 200){
                    return this.$message.error(res.msg)
                }
                
                // 将用户信息存储到本地
                localStorage.setItem('user', JSON.stringify(res.data))
                localStorage.setItem('menus', JSON.stringify(res.data.menus))

                setRoutes()

                console.log(this.$router.getRoutes())
            })
        }
    }
}
</script>

<style lang="less" scoped>
.wrapper {
    height: 100vh;
    background-image: linear-gradient(to bottom right, #446de2, #85caf6);

    /* 顶部 */
    .top {
        height: 60px;
        line-height: 60px;
        font-size: 20px;
        color: white;
        background-color: #3b5fb7;
        padding-left: 50px;
    }

    /* 主体 */
    .main {
        width: 50%;
        margin: 150px auto;
        background-color: white;
        border-radius: 10px;
        display: flex;

        /* 左侧 */
        .left {
            width: 40%;
        }

        /* 右侧 */
        .right {
            width: 350px;
            margin-top: 30px;
            margin-left: 50px;

            .title {
                text-align: center;
                font-size: 24px;
                font-weight: bold;
                letter-spacing: 6px;
                margin: 20px 0;
            }
        }
    }
}
</style>